<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属部门" prop="deptId">
        <treeselect style="width: 193px;" v-model="queryParams.deptId" :options="enabledDeptOptions" :show-count="true" placeholder="请选择部门" />
      </el-form-item>
      <el-form-item label="用户姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wx:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:user:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:wxUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList">
      <!-- <el-table-column type="selection" width="55" align="center" show-overflow-tooltip/> -->
      <el-table-column label="序号" align="center" prop="wxUserId" show-overflow-tooltip/>
      <el-table-column label="所属部门" align="center" prop="deptName" show-overflow-tooltip/>
      <el-table-column label="微信唯一标识" align="center" prop="openId" show-overflow-tooltip/>
      <!-- <el-table-column label="头像地址" align="center" prop="wxAvatar" show-overflow-tooltip/> -->
      <el-table-column label="微信昵称" align="center" prop="wxUserName" show-overflow-tooltip/>
      <el-table-column label="用户姓名" align="center" prop="realName" show-overflow-tooltip/>
      <el-table-column label="用户手机号" align="center" prop="phone" show-overflow-tooltip/>
      <el-table-column label="身份证号" align="center" prop="identityCardNum" show-overflow-tooltip/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex == 1 ? '男' : scope.row.sex == 2 ? '女' : '未知' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信登录用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="disabled">
        <el-form-item label="所属部门" prop="deptId">
          <!-- <el-input v-model="form.deptId" placeholder="请输入部门编号" /> -->
          <treeselect style="width: 100%;" :disabled="disabled" v-model="form.deptId" :options="enabledDeptOptions" :show-count="true"/>
        </el-form-item>
        <el-form-item label="微信唯一标识" prop="openId">
          <el-input v-model="form.openId" />
        </el-form-item>
        <!-- <el-form-item label="头像地址" prop="wxAvatar">
          <el-input v-model="form.wxAvatar" />
        </el-form-item> -->
        <el-form-item label="微信昵称" prop="wxUserName">
          <el-input v-model="form.wxUserName"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="realName">
          <el-input v-model="form.realName" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <!-- <el-input v-model="form.sex" placeholder="请输入用户性别" > -->
            <template slot-scope="scope">
              {{ form.sex == 1 ? '男' : form.sex == 2 ? '女' : '未知' }}
            </template>
          <!-- </el-input> -->
        </el-form-item>
        <el-form-item label="用户手机号" prop="phone">
          <el-input v-model="form.phone"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="identityCardNum">
          <el-input v-model="form.identityCardNum"/>
        </el-form-item>
        <!-- <el-form-item label="是否删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/wx/wxUser";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "User",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信登录用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        openId: null,
        wxAvatar: null,
        wxUserName: null,
        realName: null,
        phone: null,
        identityCardNum: null,
        sex: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      enabledDeptOptions: []
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询微信登录用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wxUserId: null,
        deptId: null,
        openId: null,
        wxAvatar: null,
        wxUserName: null,
        realName: null,
        phone: null,
        identityCardNum: null,
        sex: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wxUserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信登录用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wxUserId = row.wxUserId || this.ids
      getUser(wxUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.disabled = true
        this.title = "查看微信登录用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.wxUserId != null) {
      //       updateUser(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addUser(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wxUserIds = row.wxUserId || this.ids;
      this.$modal.confirm('是否确认删除微信登录用户编号为"' + wxUserIds + '"的数据项？').then(function() {
        return delUser(wxUserIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wx/wxUser/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)));
      });
    },
    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter(dept => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .vue-treeselect--disabled .vue-treeselect__control {
  background-color: #F5F7FA;
  border-color: #dfe4ed;
  color: #C0C4CC;
  cursor: not-allowed;
}
::v-deep .vue-treeselect__single-value {
  color: #C0C4CC;
}
</style>
